package com.gaga.console.core.controller.sys;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gaga.common.enums.BusinessType;
import com.gaga.common.enums.DelFlag;
import com.gaga.common.path.ApiPath;
import com.gaga.common.result.Result;
import com.gaga.console.base.annotation.Log;
import com.gaga.console.base.controller.BaseController;
import com.gaga.console.base.entity.PageParams;
import com.gaga.console.core.entity.SysUser;
import com.gaga.console.core.service.SysUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ArrayUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * 前端控制器
 *
 * @author liujia
 * @since 2022-05-19
 */
@Api(tags = "系统模块-用户管理")
@RestController
@RequestMapping(ApiPath.ROOT + "/sys/user")
public class SysUserController extends BaseController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 获取列表
     */
    @ApiOperation(value = "查询list")
    @PostMapping("/list")
    public Result list(@RequestBody PageParams<SysUser> data){
        IPage<SysUser> roleIPage = sysUserService.customPageList(data);
        return Result.ok(data.buildTableResult(roleIPage));
    }

    /**
     * 根据详细信息
     */
    @ApiOperation(value = "获取单个记录")
    @GetMapping(value = "/{id}")
    public Result getEntity(@PathVariable Long id) {
        return Result.ok(sysUserService.getById(id));
    }


    /**
     * 保存
     */
    @Log(title = "用户管理", businessType = BusinessType.INSERT_OR_UPDATE)
    @ApiOperation(value = "保存记录")
    @PostMapping("/save")
    public Result save(@Validated @RequestBody SysUser entity) {
        return toAjaxBoolean(sysUserService.customSave(entity));
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除记录")
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {

        if (ArrayUtil.isEmpty(ids)) {
            return toAjaxBoolean(false);
        }

        List<SysUser> removeEntityList = Arrays.stream(ids)
                .map(id -> {
                    SysUser entity = new SysUser().setId(id);
                    entity.setDelFlag(DelFlag.DELETED.getCode());
                    entity.setDeleteTime(new Date());
                    return entity;
                }).collect(Collectors.toList());

        return toAjaxBoolean(sysUserService.updateBatchById(removeEntityList));
    }


}
